﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.Services.Client;
using System.IO;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;
using Namespace;

namespace Demo.TableStorageDataServices.WP7
{
    public partial class AddEnigmaPage : PhoneApplicationPage, INotifyPropertyChanged
    {
        private CameraCaptureTask _cameraCaptureTask = new CameraCaptureTask();
        private Enigma _enigma = new Enigma();
        public Enigma Enigma
        {
            get { return _enigma; }
            set { _enigma = value; OnPropertyChanged("Enigma"); }
        }

        public AddEnigmaPage()
        {
            InitializeComponent();
            this.Loaded += (sender, e) => this.DataContext = this;
        }

        private void TakePhoto(object sender, RoutedEventArgs e)
        {
            _cameraCaptureTask.Completed += CameraCaptureTaskCompleted;
            _cameraCaptureTask.Show();
        }

        void CameraCaptureTaskCompleted(object sender, PhotoResult e)
        {
            if (e.ChosenPhoto == null) return;
            using (var reader = new BinaryReader(e.ChosenPhoto))
            {
                Enigma.Image = reader.ReadBytes((int)e.ChosenPhoto.Length);
            }
            _cameraCaptureTask.Completed -= CameraCaptureTaskCompleted;
        }

        private void SaveEnigma(object sender, RoutedEventArgs e)
        {
            var app = App.Current as App;


            Enigma.Latitude = app.Geolocalizer.Position.Location.Latitude;
            Enigma.Longitude = app.Geolocalizer.Position.Location.Longitude;

            if (Enigma.Latitude == Double.NaN) Enigma.Latitude = 42;
            if (Enigma.Longitude == Double.NaN) Enigma.Longitude = 42;

            Enigma.PostDate = DateTime.Now;
            Enigma.PostalCode = "75000";
            Enigma.Country = "France";


            var container = new Container(new Uri("http://wwzeclouddemo100511.cloudapp.net/Services/DemoDataService.svc/"));
            //var container = new Container(new Uri("http://127.0.0.1:81/Services/DemoDataService.svc/"));

            container.AddToEnigmas(Enigma);
            container.BeginSaveChanges(ar => Deployment.Current.Dispatcher.BeginInvoke(() =>
                                                                                           {
                                                                                               try
                                                                                               {
                                                                                                   try { container.EndSaveChanges(ar); }
                                                                                                   catch (NotSupportedException) { }
                                                                                                   MessageBox.Show("Enigma added");
                                                                                                   this.NavigationService.GoBack();
                                                                                               }
                                                                                               catch (DataServiceRequestException)
                                                                                               {
                                                                                                   MessageBox.Show("Erreur");
                                                                                               }

                                                                                           }), null);
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged(string propertyName)
        {
            if (string.IsNullOrEmpty(propertyName)) return;

            if (this.PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}